package homelesspartners.client.widgets;

import homelesspartners.client.ServerFacade;
import homelesspartners.client.model.Gift;
import homelesspartners.client.model.ShoppingList;
import homelesspartners.client.model.User;

import java.util.ArrayList;
import java.util.Iterator;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.Widget;

public class ShoppingListWidget extends Composite implements ClickListener {

	private FlexTable flexTable = new FlexTable();
	private PushButton login = new PushButton("Been here before?", this);
	private PushButton finish = new PushButton("FINISH", this);

	private ShoppingList shoppingList = null;

	private static ShoppingListWidget instance = new ShoppingListWidget();

	public static ShoppingListWidget getInstance() {
		return instance;
	}

	public ShoppingListWidget() {
		shoppingList = new ShoppingList();
		flexTable.setWidth("200px");
		flexTable.setWidget(0, 0, new Label("Your Shopping List"));
		flexTable.getFlexCellFormatter().setColSpan(0, 0, 3);
		flexTable.getFlexCellFormatter().setStylePrimaryName(0, 0, "sl-title");
		flexTable.setBorderWidth(1);
		flexTable.setCellPadding(0);
		flexTable.setCellSpacing(0);
		initWidget(flexTable);
		login.setTitle("Login to reload your shopping list");
		login.setWidth("150px");
		finish.setWidth("70px");
		refreshButtons();

	}

	public ShoppingList getShoppingList() {
		return shoppingList;
	}

	public void addGift(Gift aGift) {
		shoppingList.addGift(aGift);
		refresh();
	}

	public void addGifts(ArrayList gifts) {
		for (Iterator iter = gifts.iterator(); iter.hasNext();) {
			Gift gift = (Gift) iter.next();
			shoppingList.addGift(gift);
		}
		refresh();
	}

	private void refresh() {

		while (flexTable.getRowCount() > 1) {
			flexTable.removeRow(flexTable.getRowCount() - 1);
		}

		refreshButtons();

		int x = 2;
		for (Iterator gifts = shoppingList.getGifts().iterator(); gifts.hasNext(); x++) {
			final Gift gift = (Gift) gifts.next();
			Image image = new Image("./images/remove.jpg");
			PushButton button = new PushButton(image, new ClickListener() {

				public void onClick(Widget sender) {
					if (Window.confirm("Remove this gift from your shopping list?")) {
						ServerFacade.removePledge(gift);
					}
				}
			});
			button.setWidth("10px");
			button.setTitle("Click to remove this gift from your list");

			flexTable.setWidget(x, 0, button);
			flexTable.setWidget(x, 1, new Label(gift.getStory().getFirstName()));
			flexTable.setWidget(x, 2, new Label(gift.getDescription()));
			flexTable.getFlexCellFormatter().setWidth(x, 1, "30%");
			flexTable.getFlexCellFormatter().setWidth(x, 2, "60%");
			flexTable.getFlexCellFormatter().setStylePrimaryName(x, 1, "sl-text");
			flexTable.getFlexCellFormatter().setStylePrimaryName(x, 2, "sl-text");
		}
	}

	private void refreshButtons() {
		if (!User.getInstance().isLoggedIn()) {
			flexTable.setWidget(1, 0, login);
			flexTable.getFlexCellFormatter().setStylePrimaryName(1, 0, "sl-button");
		} else if (shoppingList.getNumberOfGifts() > 0) {
			flexTable.setWidget(1, 0, finish);
			flexTable.getFlexCellFormatter().setStylePrimaryName(1, 0, "sl-button");
		} else {
			flexTable.setWidget(1, 0, new Label("is empty"));
			flexTable.getFlexCellFormatter().setStylePrimaryName(1, 0, "sl-empty");

		}
		flexTable.getFlexCellFormatter().setColSpan(1, 0, 3);
	}

	public boolean isFull() {
		if (shoppingList.getNumberOfGifts() >= ShoppingList.MAX_NUMBER_OF_GIFTS) {
			return true;
		}
		return false;
	}

	public void onClick(Widget sender) {
		if (sender == finish) {
			GiftTabWidget.getInstance().showFinishShopping();
		} else if (sender == login) {
			LoginWidget.prompt();
		}
	}

	public void removeGift(Gift gift) {
		shoppingList.removeGift(gift);
		refresh();
	}
}
